import { Component, OnInit, ViewChild } from '@angular/core';
import { Http } from '@angular/http';
import { Router } from '@angular/router';
import { LazyLoadEvent } from 'primeng/components/common/api';
import { DataTable } from 'primeng/components/datatable/datatable';

import { ConfigService } from '../../../core/common/config.service';
import { SelectComponent } from '../../../shared/component/ng2-select/select';
import { CustomerService } from '../service/customer.service';
import { TranslateService } from '@ngx-translate/core';
import { ModalDirective } from 'ngx-bootstrap';
import { NodeComponent } from '../component/node/node.component';
@Component({
    selector: 'app-myorder',
    templateUrl: './myorder.component.html',
    styleUrls: ['./myorder.component.scss']
})
export class MyorderComponent implements OnInit {
    @ViewChild('orderStateSelect') public orderStateSelect: SelectComponent;
    @ViewChild('md') public md: DataTable;
    @ViewChild('nodeModal') public nodeModal: ModalDirective;
    @ViewChild('ncloudnode') public ncloudnode: NodeComponent;
    public customer: any = {
        custName: '',
        orderState: '0'
    };
    public flag = false;
    public ensure: any = {
        custName: '',
        orderState: '0'
    };
    public myorder: Array<any>;
    public totalRecords: number;
    public pageSize = 10;

    public customers: Array<any>;
    public nodeInfos: Array<any> = [];

    constructor(public configService: ConfigService, public http: Http, public router: Router,
        private customerService: CustomerService, private translate: TranslateService) {

    }
    ngOnInit() {
        this.translate.get('crm.myorder.status').subscribe((res: any) => {
            this.orderStateSelect.items = [{ id: '0', text: res.all }, { id: '2', text: res.unsuccess }, { id: '3', text: res.success }];
        });
    }
    public loadLazy(event: LazyLoadEvent) {
        // setTimeout(() => {
        const pageNum = event.first / event.rows + 1;
        const pageSize = event.rows;
        const custName = this.flag ? this.customer.custName : this.ensure.custName;
        const orderState = this.flag ? this.customer.orderState : this.ensure.orderState;
        this.customerService.getMyorders(custName, orderState, pageNum, pageSize).subscribe(page => {
            // console.log(page.list);
            this.myorder = page.list;
            this.totalRecords = page.total;
        });
        // }, 250);
    }
    public submitQuery() {
        this.flag = true;
        this.md.reset();
        const custName = this.flag ? this.customer.custName : this.ensure.custName;
        const orderState = this.flag ? this.customer.orderState : this.ensure.orderState;
        this.customerService.getMyorders(custName, orderState, 1, this.pageSize).subscribe(page => {
            this.myorder = page.list;
            // console.log(page.list);
            this.totalRecords = page.total;
            Object.assign(this.ensure, this.customer);
            this.flag = false;
        });
    }
    public lookupOrderInfos(order): void {
        // console.log(order);
        this.customerService.getNodesByOrder(order.orderId).subscribe((nodeInfos: any) => {
            // console.log(nodeInfos);
            if (this.ncloudnode && this.ncloudnode.nodeTab) {
                this.ncloudnode.nodeTab.remove();
            }
            this.nodeInfos = [];
            nodeInfos.forEach((n: any) => {
                n.active = true;
                if (n.isp) {
                    n.isp.isDisabled = true;
                }
                n.isDisabled = true;
                this.nodeInfos.push(n);
            });
            setTimeout(_ => this.nodeModal.show(), 250);
        });
    }
}
